package flc.ast.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.RecordActivity;
import flc.ast.activity.TemplateActivity;
import flc.ast.adapter.HomeRecordAdapter;
import flc.ast.bean.RecordBean;
import flc.ast.databinding.FragmentAvatarBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AvatarFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AvatarFragment extends BaseNoModelFragment<FragmentAvatarBinding> {

    private HomeRecordAdapter recordAdapter;
    private List<String> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_avatar;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivTemplateType1.setOnClickListener(this);
        mDataBinding.ivTemplateType2.setOnClickListener(this);
        mDataBinding.ivTemplateType3.setOnClickListener(this);
        mDataBinding.ivTemplateType4.setOnClickListener(this);
        mDataBinding.tvRecordAll.setOnClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new GridLayoutManager(mContext, 2));
        recordAdapter = new HomeRecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvRecordAll://制作记录
                startActivity(RecordActivity.class);
                break;
            case R.id.ivTemplateType1://古风
                TemplateActivity.selPosition = 0;
                startActivity(TemplateActivity.class);
                break;
            case R.id.ivTemplateType2://华丽
                TemplateActivity.selPosition = 1;
                startActivity(TemplateActivity.class);
                break;
            case R.id.ivTemplateType3://甜美
                TemplateActivity.selPosition = 2;
                startActivity(TemplateActivity.class);
                break;
            case R.id.ivTemplateType4://校园
                TemplateActivity.selPosition = 3;
                startActivity(TemplateActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecord;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        if (listShow.size() > 0) {
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.ivRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.ivRecordNoData.setVisibility(View.VISIBLE);
        }
    }

}